package com.friday.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.friday.bo.PersonnelBo;
import com.friday.bo.QueryAttendanceBo;
import com.friday.bo.impl.PersonnelBoImpl;
import com.friday.bo.impl.QueryAttendanceBoImpl;
import com.friday.bo.impl.UtilBo;
import com.friday.po.Department;
import com.friday.po.EmployeeAttendance;

public class QueryDepartmentAttendanceServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		QueryAttendanceBo queryAttendanceBo = new QueryAttendanceBoImpl();
		HttpSession session = request.getSession();
		String id = (String)session.getAttribute("id");
		String dep = (String)request.getParameter("department");
		String sd = (String)request.getParameter("startdate");
		String ed = (String)request.getParameter("enddate");
		List<EmployeeAttendance> employeeAttendances = null;
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		int actor = (Integer)session.getAttribute("actor");
		PersonnelBo personnelBo = new PersonnelBoImpl();
		
		try {
			List<Department> deps = personnelBo.getAllDepartment();
			session.setAttribute("deps", deps);
			if(dep == null) {
				if (actor == 1) {
					dep = UtilBo.queryDepartmentByEmployeeId(id);
				}
				else if(actor == 2) {
					dep = null;
				}
				if(sd == null || ed == null || sd.equals("") || ed.equals("")) {
					employeeAttendances = queryAttendanceBo.queryHistoriesByDepartmentAndDate(dep, null, null);
				}
				else {
					java.util.Date date = bartDateFormat.parse(sd);
			        java.sql.Date sDate = new java.sql.Date(date.getTime());
			        date = bartDateFormat.parse(ed);
			        java.sql.Date eDate = new java.sql.Date(date.getTime());
			        employeeAttendances = queryAttendanceBo.queryHistoriesByDepartmentAndDate(dep, sDate, eDate);
				}
			}
			else {
				if(sd == null || ed == null || sd.equals("") || ed.equals("")) {
					employeeAttendances = queryAttendanceBo.queryHistoriesByDepartmentAndDate(dep, null, null);
				}
				else {
					java.util.Date date = bartDateFormat.parse(sd);
			        java.sql.Date sDate = new java.sql.Date(date.getTime());
			        date = bartDateFormat.parse(ed);
			        java.sql.Date eDate = new java.sql.Date(date.getTime());
			        employeeAttendances = queryAttendanceBo.queryHistoriesByDepartmentAndDate(dep, sDate, eDate);
				}
			}
			
			session.setAttribute("queryattendance", employeeAttendances);
			
			if (actor == 1) {
				response.sendRedirect("Manager_staff_CheckInDetails.jsp");
			}
			else if(actor == 2) {
				response.sendRedirect("All_staff_CheckInDetails.jsp");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
